package com.lyj.mall.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lyj.mall.consts.MethodConst;
import com.lyj.mall.form.UserLoginForm;
import com.lyj.mall.form.UserRegisterForm;
import com.lyj.mall.pojo.User;
import com.lyj.mall.service.UserService;
import com.lyj.mall.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static com.lyj.mall.enums.ResponseEnum.PARAMETER_ERROR;

/**
 * @author lyj
 * @date 2019/12/18 23:29  subject.getPrincipal()获取的参数就是realm中new SimpleAuthenticationInfo传入的第一个参数!!!
 */
@Api(tags = "用户模块Controller")
@RestController
@RequestMapping(value = "/user")
@Slf4j
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "/register")
    @ApiOperation(value = "register", notes = "用户注册接口", httpMethod = "POST")
    @ApiImplicitParam(name = "userRegisterForm", value = "用户注册表单,json格式", required = true)
    public ResponseVo register(@Valid @RequestBody UserRegisterForm userRegisterForm) {
        this.insertLog("用户注册", MethodConst.INSERT, userRegisterForm.toString());
        User user = new User();
        BeanUtil.copyProperties(userRegisterForm, user);
        return userService.register(user);
    }

    @PostMapping(value = "/login")
    @ApiOperation(value = "login", notes = "用户登入接口", httpMethod = "POST")
    @ApiImplicitParam(name = "userLoginForm", value = "用户登入表单,json格式", required = true)
    public ResponseVo login(@Valid @RequestBody UserLoginForm userLoginForm) {
        this.insertLog("用户登入", MethodConst.QUERY, userLoginForm.toString());
        UsernamePasswordToken token = new UsernamePasswordToken(userLoginForm.getUsername(), userLoginForm.getPassword());
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        User user = (User) subject.getPrincipal();
        log.info("登入成功");
        user.setPassword("");
        /*  设置session过期时间（也是保持登入状态的时间） tomcat默认30分钟
        Session session = subject.getSession();
        session.setTimeout(10000); //  毫秒值,等于10s
        */
        return ResponseVo.success(user);
    }

    @GetMapping(value = "/getUser")
    public ResponseVo getUser(HttpServletRequest request) {
        this.insertLog("获取用户信息", MethodConst.QUERY, "");
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String cookieName = cookie.getName();
            String cookieValue = cookie.getValue();
            log.info("cookie的name: {},value: {}", cookieName, cookieValue);
        }
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        return ResponseVo.success(user);
    }

    @PostMapping(value = "/logout")
    public ResponseVo logout() {
        Subject subject = SecurityUtils.getSubject();
        this.insertLog("退出登入", MethodConst.QUERY, subject.getPrincipal().toString());
        subject.logout();
        return ResponseVo.successByMsg("退出成功");
    }

}
